﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/4/7 星期日 20:44:42
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;

namespace Sector7.Framework.DataDictionary.Agents
{
    /// <summary>
    /// 值域服务代理
    /// </summary>
    public class ValueDomainServiceAgent
    {
        /// <summary>
        /// 添加值域
        /// </summary>
        /// <param name="domain">值域</param>
        /// <returns>操作结果</returns>
        public static int Add(S7_ValueDomain domain)
        {
            object result = WcfClient.GetInstance().Invoke<object>(
                service => service.AddValueDomain(domain));

            return Convert.ToInt32(result);
        }

        /// <summary>
        /// 修改值域
        /// </summary>
        /// <param name="delta">值域改变量</param>
        /// <returns>操作结果</returns>
        public static int Set(EntityDeltaBase delta)
        {
            return WcfClient.GetInstance().Invoke<int>(
                service => service.SetValueDomain(delta));
        }

        /// <summary>
        /// 根据数据分组编号获取所有值域
        /// </summary>
        /// <returns>所有值域列表</returns>
        public static List<S7_ValueDomain> GetAll()
        {
            List<S7_ValueDomain> domains = null;

            try
            {
                domains = WcfClient.GetInstance().Invoke<List<S7_ValueDomain>>(
                    service => service.GetAllValueDomains());
            }
            catch (Exception e)
            {
                Log.WriteLine(LogSeverityType.WRN, "DAD", e);
            }

            return domains;
        }

        /// <summary>
        /// 根据数据分组编号获取所有值域
        /// </summary>
        /// <param name="groupId">数据分组编号</param>
        /// <returns>所有值域列表</returns>
        public static List<S7_ValueDomain> GetAll(int groupId)
        {
            List<S7_ValueDomain> domains = null;

            try
            {
                domains = WcfClient.GetInstance().Invoke<List<S7_ValueDomain>>(
                    service => service.GetAllValueDomainsByGroupId(groupId));
            }
            catch (Exception e)
            {
                Log.WriteLine(LogSeverityType.WRN, "DAD", e);
            }

            return domains;
        }

        /// <summary>
        /// 删除值域
        /// </summary>
        /// <param name="domainId">值域编号</param>
        /// <returns>操作结果</returns>
        public static int Delete(string domainId)
        {
            return WcfClient.GetInstance().Invoke<int>(
                service => service.DeleteValueDomain(domainId));
        }
    }
}
